package com.xzc.java8.A_lambda.lambda6;

import java.util.Comparator;

import static java.util.Comparator.comparing;

public class example1 {

    public static void sort(Comparator<Apple> c) {
    }

    //0-base
    static class AppleComparator implements Comparator<Apple> {
        @Override
        public int compare(Apple a1, Apple a2) {
            return a1.getWeight() - a2.getWeight();
        }
    }

    public static void main(String[] args) {
        //0
        sort(new AppleComparator());

        //1-anonymouse class
        Comparator<Apple> anonayClass = new Comparator<Apple>() {
            @Override
            public int compare(Apple a1, Apple a2) {
                return a1.getWeight() - a2.getWeight();
            }
        };
        sort(anonayClass);

        //2-lambda
        sort((Apple a1, Apple a2) -> a1.getWeight() - a2.getWeight());

        //3-lambda use infer the types,这个地方必须让sort指明入参类型才可
        sort((a1, a2) -> a1.getWeight() - a2.getWeight());

        //4-lambda use comparator static method comparing product a Comparator
        //Function被看成是一个key的抽取器，从a中抽取weight，然后进行比较
        sort(comparing((Apple a) -> a.getWeight()));

        //5-lambda 简化key抽取器
        sort(comparing((a) -> a.getWeight()));

        //6-functional interface，读起来非常自然了，但调试上。。。
        sort(comparing(Apple::getWeight));

    }
}
